#include <uci.h>
#include "data_type.h"
#include "idc_ubus.h"
#include "ep_gpio.h"
#include "mqtt_service.h"
#include "hash_map.h"

#define UCI_CONFIG_FILE "/etc/config/idc_mqtt"
#define NUM_THREADS             6

extern int server_num;
extern MQTTServer servers[10];
t_RunCfg runCfg;
HashMap *config_devs;

void idc_load_config();

void init(){
    // ep_gpio_export(448);
    // ep_gpio_direction(448,2);
    // 创建Map
    
    config_devs = map_create(INITIAL_CAPACITY);
    if (!config_devs) {
        printf("❌ 创建Map失败\n");
        return;
    }
    map_print_status(config_devs);
    idc_load_config();
}


int main(int argc, char* argv[])
{
    init();
    pthread_t th_ubus;
    pthread_create(&th_ubus, NULL, (void*)ubus_worker, NULL);
    while (true)
    {
        sleep(1);
    }
}

/*********************************************
* 载入配置文件,并遍历Section.
*/
void idc_load_config()
{
    struct uci_package * pkg = NULL;
    struct uci_element *e;
    struct uci_context * ctx = NULL;
    char *tmp;
    const char *value;

    ctx = uci_alloc_context(); // 申请一个UCI上下文.
    if (UCI_OK != uci_load(ctx, UCI_CONFIG_FILE, &pkg))
        goto cleanup; //如果打开UCI文件失败,则跳到末尾 清理 UCI 上下文.


    /*遍历UCI的每一个节*/
    uci_foreach_element(&pkg->sections, e)
    {
        struct uci_section *s = uci_to_section(e);

        printf("section s's type is %s.\n",s->type);
        if(!strcmp("conf",s->type)) //this section is a meter
        {
            if (NULL != (value = uci_lookup_option_string(ctx, s, "sn")))
            {
                printf("sn:%s\n", strdup(value));
                runCfg.sn = strdup(value);
            }
        }
        if(!strcmp("server",s->type)) //this section is a meter
        {
            if (NULL != (value = uci_lookup_option_string(ctx, s, "enable")))
            {
                printf("enable:%s\n", strdup(value));
                if (strcmp(value, "1") == 0 || strcmp(value, "true") == 0) {
                    servers[server_num].enable = true;
                } else if (strcmp(value, "0") == 0 || strcmp(value, "false") == 0) {
                    servers[server_num].enable = false;
                }
            }
            if (NULL != (value = uci_lookup_option_string(ctx, s, "host")))
            {
                printf("host:%s\n", strdup(value));
                servers[server_num].host = strdup(value);
            }
            if (NULL != (value = uci_lookup_option_string(ctx, s, "port")))
            {
                printf("port:%d\n", atoi(strdup(value)));
                servers[server_num].port = atoi(strdup(value));
            }
            if (NULL != (value = uci_lookup_option_string(ctx, s, "username")))
            {
                printf("username:%s\n", strdup(value));
                servers[server_num].username = strdup(value);
            }
            if (NULL != (value = uci_lookup_option_string(ctx, s, "password")))
            {
                printf("password:%s\n", strdup(value));
                servers[server_num].password = strdup(value);
            }
            if (NULL != (value = uci_lookup_option_string(ctx, s, "pubtopic")))
            {
                printf("pubtopic:%s\n", strdup(value));
                servers[server_num].pubtopic = strdup(value);
            }
            if (NULL != (value = uci_lookup_option_string(ctx, s, "subtopic")))
            {
                printf("subtopic:%s\n", strdup(value));
                servers[server_num].subtopic = strdup(value);
            }
            server_num++;
        }
        if(!strcmp("device",s->type)) //this section is a meter
        {
            const char *device_sn, *energy_sn;
            
            if (NULL != (device_sn = uci_lookup_option_string(ctx, s, "device_sn")) \
            && NULL != (energy_sn = uci_lookup_option_string(ctx, s, "energy_sn")))
            {
                printf("device_sn:%s, energy_sn:%s\n", strdup(device_sn), strdup(energy_sn));
                map_put(config_devs, strdup(energy_sn), strdup(device_sn), str_free);
                map_print_status(config_devs);
            }
        }
    }
    uci_unload(ctx, pkg); // 释放 pkg
    cleanup:
    uci_free_context(ctx);
    ctx = NULL;
}


